package main

import (
	"bufio"
	"fmt"
	"io/ioutil"
	"net/http"
	"strings"
)

const (
	REQ_URL = "http://pv.sohu.com/cityjson?ie=utf-8"
)

// 读取 resp 中的全部内容
func req1(res chan string) {
	resp, err := http.Get(REQ_URL)
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()
	fmt.Println("Response status:", resp.Status)
	// resp.Body
	byteBody, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return
	}
	res <- "============================== Response:" + string(byteBody) + "============================== \n"
}
// 读取 resp 中的部分内容
func req2(res chan string) {
	resp, err := http.Get(REQ_URL)
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()
	fmt.Println("Response status:", resp.Status)

	// 读取部分内容
	scanner := bufio.NewScanner(resp.Body)
	var buf strings.Builder
	for i := 0; scanner.Scan() && i < 5; i++ {
		buf.Write(scanner.Bytes())
	}
	res <- "============================== Response:" + buf.String() + "============================== \n"
	if err := scanner.Err(); err != nil {
		panic(err)
	}
}
func main() {
	ch := make(chan string, 2)
	go req1(ch)
	go req2(ch)
	//close(ch)
	//for i := range ch{
	//	fmt.Println(i)
	//}
	fmt.Println(<-ch)
	fmt.Println(<-ch)
}
